/* Endless Code Copyright (C) 2009 Jingul,Kim
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
package ec.core;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import ec.core.event.MouseEvt;
import ec.gfx.GL;
import ec.gfx.Scene;
import ec.util.Point;

/**
 * 여러 상태에 따라 마우스의 이벤트를 제어하고 전달합니다.
 * 여러가지 설정 값을 조율하면서 이벤트 발생을 제어할 수 있습니다.
 * 
 * @since 2009
 * @version 1.0
 * @author <a href="mailto:jingul.kim@gmail.com">Jingul,Kim</a>
 */
public class Mouse extends Input implements Staff {

	private long pre_click;
	private long pre_down;

	// button delay
	private long click = 150;
	private long dbclick = 200;

	private Scene scene;

	private IntBuffer buffer;

	private static final int BUFFER_SIZE = 512;

	private boolean isDowned;

	public Mouse() {
		settingBuffer();

		GL.selectBuffer(BUFFER_SIZE, buffer);
	}

	public int checkUpPeriod() {
		final long now = System.currentTimeMillis();

		// 이전의 눌름 이벤트와 올림 이벤트의 시간차로 클릭이벤트인지 판별한다.
		if (now - pre_down < click) {

			if (pre_click != 0) {
				
				//이전의 클릭 이벤트간의 시간차로 더블이벤트인지 판별 
				if (now - pre_click < dbclick) {
					pre_click = 0;
					return MouseEvt.DBCLICK;
				}
			}

			pre_click = now;
			return MouseEvt.CLICK;
		}

		return MouseEvt.UP;
	}

	public void dispatch(int type, int button, int x, int y) {
		final int hits = scene.picking(x, y);

		switch (type) {
		case MouseEvt.UP:
			isDowned = false;
			break;
		case MouseEvt.DOWN:
			pre_down = System.currentTimeMillis();
			isDowned = true;
			break;
		case MouseEvt.MOVE:
			if (isDowned) {
				type = MouseEvt.DRAG;
			}
			break;
		default:
			break;
		}

		if (hits > 0) {
			Point p = new Point();

			scene.realToWorld(p, x, y);
			Caster caster = getCaster(buffer.get(3 + 4 * (hits -1)));

			if (caster != null) {
				do {
					caster.processEvent(new MouseEvt(caster, type, button,
							p.m_x, p.m_y));

					if (type == MouseEvt.UP) {
						type = checkUpPeriod();
						
					} else {
						break;
					}
				} while (type > 3);
			}

		}
	}

	private void settingBuffer() {
		ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE * Integer.SIZE
				/ Byte.SIZE);
		buf.order(ByteOrder.nativeOrder());

		buffer = buf.asIntBuffer();
	}

	@Override
	public void changedScene(Scene scene) {
		this.scene = scene;
	}
}
